/*
 @description: 分类管理
*/
'use strict';
const Controller = require('egg').Controller;
class CategoryController extends Controller {
    async index() { //搜索
        const {
            ctx,
            app
        } = this;

        let result = await app.mysql.select('category');
        // console.log(result);
        if (result) {
            // console.log(5454);
            ctx.body = {
                code: 0,
                msg: '获取成功',
                data: result
            }
        } else {
            // console.log(123);
            ctx.body = {
                code: 4004,
                msg: '获取失败'
            }
        }
    }
    //通过id 来获取选到的数据
    async show() {
        const {
            ctx,
            app
        } = this;
        const {
            id
        } = ctx.params;
        // 根据id
        const result = await app.mysql.get('category', {
            ID: id
        })
        if (result) {
            ctx.body = {
                code: 0,
                msg: '查找成功',
                data: result
            };
        } else {
            ctx.body = {
                code: 4007,
                msg: "查找失败"
            }
        }
    }
    //添加
    async create() { //post
        const {
            ctx,
            app
        } = this;
        const {
            class_name,
            class_image
        } = ctx.request.body;
        console.log(class_name, class_image);
        if (class_name && class_image) {
            const data = await app.mysql.insert('category', {
                class_name,
                class_image
                // nickname: Date.now()
            });
            ctx.body = {
                code: 0,
                msg: "新增成功",
            };
        } else {
            ctx.body = {
                code: 4006,
                msg: "添加失败",
            };
        }
    }
    //改
    async update() { //put
        const {
            ctx,
            app
        } = this;
        const {
            class_name,
            class_image
        } = ctx.request.body; //传进来的数据
        console.log(class_name,
            class_image);
        const {
            id
        } = ctx.params;
        const row = {
            class_name,
            class_image
        };
        const options = { //通过这个id 来选定的修改数据
            where: {
                id: +id
            }
        };
        const result = await app.mysql.update('category', row, options);
        if (result) {
            ctx.body = {
                code: 0,
                msg: "编辑成功",
            };
        } else {
            ctx.body = {
                code: 4008,
                msg: "编辑失败",
            };
        }
    }
    //删除
    async destroy() { //DELETE
        const {
            ctx,
            app
        } = this;
        const {
            id
        } = ctx.params;
        const result = await app.mysql.delete(`category`, {
            id: +id
        })
        if (result) {
            ctx.body = {
                code: 0,
                msg: "删除成功",
            };
        } else {
            ctx.body = {
                code: 4007,
                msg: "删除失败",
            };
        }
    }
}
module.exports = CategoryController;